package org.squabble.validator;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.squabble.service.AccountService;
import org.squabble.web.admin.AccountSearchCommand;

@Component(value = "accountSearchValidator")
public class AccountSearchValidator implements Validator {

	@Autowired
	AccountService accountService;

	@SuppressWarnings("unchecked")
	@Override
	public boolean supports(Class clazz) {
		return AccountSearchCommand.class.isAssignableFrom(clazz);
	}

	@Override
	public void validate(Object arg0, Errors errors) {
		AccountSearchCommand asc = (AccountSearchCommand) arg0;
		if (asc.getPrivilegeId() == null && StringUtils.isBlank(asc.getEmail())
				&& StringUtils.isBlank(asc.getName())) {
			errors.rejectValue("name", "AccountSearchCommand.name[no.params]");
			return;
		}
		if (StringUtils.isNotBlank(asc.getName()) && asc.getName().length() < 3) {
			errors.rejectValue("name", "AccountSearchCommand.name[length]",
					new Object[] { 3 }, null);
		}
		if (StringUtils.isNotBlank(asc.getEmail())
				&& asc.getEmail().length() < 3) {
			errors.rejectValue("name", "AccountSearchCommand.email[length]",
					new Object[] { 3 }, null);
		}
	}

}
